/**
 * @author 太阳当空丶赵
 * @date 2022/12/2-17:01
 * @version 1.0.0
 * feedbackRouter
 */
const express = require("express")
const getUUID = require('../utils/getUUID')
const getTime = require('../utils/getTime')
const BaseResult = require('../utils/result/BaseResult')
const {insertOne, selectFeedbackReply} = require('../database/mapper')
const expressJoi = require('@escook/express-joi')
const {replyRules, replyContentRules} = require('../utils/rules/replyRules')

const feedbackRouter = express.Router()


/**
 * 回复反馈内容
 * */
feedbackRouter.post("/addReply",expressJoi(replyContentRules), async (req, res) => {
    const {userId} = req.auth
    const {feedbackId, replyContent} = req.body
    try {
        const data = {
            reply_id: await getUUID(),
            reply_content: replyContent,
            feedback_id: feedbackId,
            user_id: userId,
            reply_time: getTime()
        }
        await insertOne("reply", data)
        return res.send(BaseResult.success("success"))
    }catch (e) {
        console.log(e)
        return res.send(BaseResult.validateFailed())
    }

})

/**
 * 根据反馈id查询反馈的回复内容
 * */
feedbackRouter.post("/getFeedbackReply",expressJoi(replyRules),async (req, res) => {
    const {feedbackId} = req.body
    try {
        const result = await selectFeedbackReply(feedbackId)
        return res.send(BaseResult.success(result))
    }catch (e){
        return res.send(BaseResult.error())
    }
})


module.exports = feedbackRouter
